$(function(){
	var basePath=$("#basepath").val();
	$(".course_edit").click(function(){
		var courseId=$(this).attr("data-courseId");
		$.get(basePath+ "admin/teach/getCourseByCourseId/"+courseId,{},
			function(data,textStatus){
				if(textStatus=="success"){
					if(data.error>0||data==null||data==""){
						alert("登陆状态已过期,请重新登陆");
						window.location=basePath;
						return;
					}
					$("#course_edit_box input[name='id']").val(data.id);
					$("#course_edit_box input[name='courseId']").val(data.courseId);
					$("#course_edit_box input[name='name']").val(data.name);
					$("#course_edit_box input[name='term']").val(data.term);
					$("#course_edit_box input[name='credit']").val(data.credit);
					$("#course_edit_box input[name='category']").val(data.category);
					$("#course_edit_box input[name='weeknum']").val(data.weeknum);
				}
		}, "json");
	});
	$("#course_edit_box_save").click(function(){
		var id=$("#course_edit_box input[name='id']").val();
		var courseId=$("#course_edit_box input[name='courseId']").val();
		var name=$("#course_edit_box input[name='name']").val();
		var term=parseInt($("#course_edit_box input[name='term']").val(),10);
		var credit=parseInt($("#course_edit_box input[name='credit']").val(),10);
		var category=$("#course_edit_box input[name='category']").val();
		var weeknum=parseInt($("#course_edit_box input[name='weeknum']").val(),10);
		if(isNaN(term)){
			alert("行课学期请输入数字");
			return;
		}	
		if(isNaN(credit)){
			alert("课程学分请输入数字");
			return;
		}
		if(isNaN(weeknum)){
			alert("行课周数请输入数字");
			return;
		}
		$.post(basePath+ "admin/teach/updateCourse",
				{'id':id,'courseId':courseId,'name':name,'term':term,'credit':credit,'category':category,'weeknum':weeknum},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("所有字段均为必填项,请检查");
							break;
						case -2:
							alert("已存在编号为:"+courseId+" 的课程,请重新输入");
							break;
						case 0:
							noty({text:"修改成功,正在刷新"});
							location.reload();
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
				},"text");
	});
	$(".course_file_edit").click(function(){
		var courseId=$(this).attr("data-courseId");
		$("#course_file_upload_box input[name='id'").val(courseId);
		$("#course_file_upload_box_file_confirm").unbind();
		$("#course_file_upload_box_file_name").html("未选择");
		$("#course_file_upload_box_alert_container").html("");
		$("#course_file_upload_box_file").show();
		$("#course_file_upload_box_progress").hide();
		$("#course_file_upload_box_progress div").css("width","0%");
	});
	$("#course_file_upload_box_file input[type='file']").fileupload({  
	    autoUpload:false,
	    done:function(e,result){  
			noty({text:"上传完成,正在刷新"});
			location.reload();
	    } ,
	    progress:function(e,result){
	    	var progress=parseInt(result.loaded / result.total * 100, 10);
	    	$("#course_file_upload_box_progress div").css("width",progress+"%");
	    },
	    add:function(e,result){
	    	var fileName=result.files[0].name.toLowerCase();
	    	$("#course_file_upload_box_file_name").html(fileName);
			$("#course_file_upload_box_file_confirm").unbind();
			$("#course_file_upload_box_file_confirm").click(function(){
				$("#course_file_upload_box_file_name").html("正在上传:"+fileName);
				$("#course_file_upload_box_file").hide();
				$("#course_file_upload_box_progress").show();
				var courseId=$("#course_file_upload_box input[name='id'").val();
				result.url=basePath+"admin/teach/updateCoursePlan/"+courseId;
				result.submit();
			});
	    }
	}) ;
	$(".course_delete").click(function(){
		var courseId=$(this).attr("data-courseId");
		$("#course_delete_box input[name='id'").val(courseId);
	});
	$("#course_delete_box_delete").click(function(){
		var courseId=$("#course_delete_box input[name='id'").val();
		$.get(basePath+ "admin/teach/deleteCourse/"+courseId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case 0:
							noty({text:"删除成功,正在刷新"});
							location.reload();
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#course_add").click(function(){
		$("#course_add_box input[name='courseId']").val("");
		$("#course_add_box input[name='name']").val("");
		$("#course_add_box input[name='term']").val("");
		$("#course_add_box input[name='credit']").val("");
		$("#course_add_box input[name='category']").val("");
		$("#course_add_box input[name='weeknum']").val("");
	});
	$("#course_add_box_save").click(function(){
		var courseId=$("#course_add_box input[name='courseId']").val();
		var name=$("#course_add_box input[name='name']").val();
		var term=parseInt($("#course_add_box input[name='term']").val(),10);
		var credit=parseInt($("#course_add_box input[name='credit']").val(),10);
		var category=$("#course_add_box input[name='category']").val();
		var weeknum=parseInt($("#course_add_box input[name='weeknum']").val(),10);
		if(isNaN(term)){
			alert("行课学期请输入数字");
			return;
		}	
		if(isNaN(credit)){
			alert("课程学分请输入数字");
			return;
		}
		if(isNaN(weeknum)){
			alert("行课周数请输入数字");
			return;
		}
		$.post(basePath+ "admin/teach/insertCourse",
				{'courseId':courseId,'name':name,'term':term,'credit':credit,'category':category,'weeknum':weeknum},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("所有字段均为必填项,请检查");
							break;
						case -2:
							alert("已存在编号为:"+courseId+" 的课程,请重新输入");
							break;
						case 0:
							noty({text:"添加成功,正在刷新"});
							location.reload();
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
				},"text");
	});
	$("#teachCourseFilter_go").click(function(){
		var clgId=$("#teachCourseFilter_form select[name='college']").val();
		if(clgId==0){
			return;
		}
		window.location=basePath+"admin/teach/showTeachCourseList/"+clgId+"/1";
	});
	$(".tc_delete").click(function(){
		var tcId=$(this).attr("data-tcId");
		$("#tc_delete_box input[name='id'").val(tcId);
	});
	$("#tc_delete_box_delete").click(function(){
		var tcId=$("#tc_delete_box input[name='id'").val();
		$.get(basePath+ "admin/teach/deleteTeachCourse/"+tcId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case 0:
							noty({text:"删除成功,正在刷新"});
							location.reload();
							break;
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#tc_add").click(function(){
		var clgId=$("#tc_add_box input[name='clgId']").val();
		$("#tc_add_box select[name='college']").val(clgId);
		$("#tc_add_box select[name='college']").change()
	});
	$("#tc_add_box select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#tc_add_box select[name='department']");
		$depSelect.html("");
		var $teacherSelect=$("#tc_add_box select[name='teacher']");
		$teacherSelect.html("");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($depSelect);
							$(" <option value='0'>不可用</option>").appendTo($teacherSelect);
							alert("无可用系,请添加之后再试");
						}else{
							$depSelect.change();
						}
					}
			}, "json");
	});
	$("#tc_add_box select[name='department']").change(function(){
		var $teacherSelect=$("#tc_add_box select[name='teacher']");
		$teacherSelect.html("");
		var depId=$(this).val();
		$.get(basePath+ "admin/teacher/getAllTeacherByDep/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							var $option=$("<option/>").html(data[i].name).attr("value",data[i].id);
							$option.appendTo($teacherSelect);
						}
						if(data.length==0){
							$(" <option value='0'>不可用</option>").appendTo($teacherSelect);
							alert("无可用教师,请添加之后再试");
						}
					}
			}, "json");
	});
	$("#tc_add_box_save").click(function(){
		var teaId=$("#tc_add_box select[name='teacher']").val();
		var courseId=$("#tc_add_box select[name='course']").val();
		var address=$("#tc_add_box input[name='address']").val();
		if(teaId==0){
			alert("无可用教师,请添加之后再试");
			return;
		}
		$.post(basePath+ "admin/teach/insertTeachCourse",
				{'tea_id':teaId,'course_id':courseId,'address':address},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case -2:
							alert("所有字段均为必填项,请检查");
							break;
						case 0:
							noty({text:"添加成功,正在刷新"});
							location.reload();
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
				},"text");
	});
});